﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.HtmlControls;

namespace SmartSpace.Web
{
    public class SsPart : UserControl
    {
        protected SsModule _ssModule;

        public SsModule SsModule
        {
            get
            { return _ssModule; }
        }

        public Guid PartId { get; private set; }

        public SsPart(Guid id)
        {
            PartId = id;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                OnLoad(sender, e);
            }
            catch
            {
               
            }
        }

        protected virtual void OnLoad(object sender, EventArgs e)
        {
        }
        
        /// <summary>
        /// 注册css文件
        /// </summary>
        /// <param name="key"></param>
        /// <param name="relativePath"></param>
        public void RegisterCss(string key, string relativePath)
        {
            foreach (Control item in this.Page.Header.Controls)
            {
                if (item.ID == key)
                {
                    return;
                }
            }

            string resolvedPath = relativePath;
            if (resolvedPath.StartsWith("./"))
            {
                resolvedPath = resolvedPath.Substring(2);
            }
            else if (resolvedPath.StartsWith("//"))
            {
                resolvedPath = resolvedPath.Substring(2);
            }
            else if (resolvedPath.StartsWith("/"))
            {
                resolvedPath = resolvedPath.Substring(1);
            }

            HtmlLink link = new HtmlLink();
            link.ID = string.Format("css_{0}_{1}", _ssModule.Id, key);
            link.Attributes.Add("type", "text/css");
            link.Attributes.Add("rel", "stylesheet");
            link.Attributes.Add("href", this.ResolveUrl(string.Format("~/_modules/{0}/{1}", _ssModule.Code, resolvedPath)));
            this.Page.Header.Controls.Add(link);
        }

        /// <summary>
        /// 注册脚本
        /// </summary>
        /// <param name="key"></param>
        /// <param name="relativePath"></param>
        public void RegisterJs(string key, string relativePath)
        {
            foreach (Control item in this.Page.Header.Controls)
            {
                if (item.ID == key)
                {
                    return;
                }
            }

            string resolvedPath = relativePath;
            if (resolvedPath.StartsWith("./"))
            {
                resolvedPath = resolvedPath.Substring(2);
            }
            else if (resolvedPath.StartsWith("//"))
            {
                resolvedPath = resolvedPath.Substring(2);
            }
            else if (resolvedPath.StartsWith("/"))
            {
                resolvedPath = resolvedPath.Substring(1);
            }

            HtmlGenericControl script = new HtmlGenericControl("script");
            script.ID = string.Format("js_{0}_{1}", _ssModule.Id, key);
            script.Attributes.Add("type", "text/javascript");
            script.Attributes.Add("src", this.Page.ResolveUrl(string.Format("~/_modules/{0}/{1}", _ssModule.Code, resolvedPath)));
            this.Page.Header.Controls.Add(script);
        }

        public static void SetSsModule(SsPart part, SsModule module)
        {
            part._ssModule = module;
        }
    }
}
